#include<iostream>
#include<cstring>
#include<ctime>
#include<stdlib.h>
#include"socket.hpp"
#include<assert.h>
#include"protocol.hpp"
using namespace std;
int main(int argc,char*argv[])
{

if(argc!=3)
{

cout<<"please :./clientcal ip port"<<endl;
exit(0);
}

uint16_t port=stoi(argv[2]);
string ip=argv[1];
sock sk;
  sk.Socket();
  bool r=sk.Connect(port,ip);
  if(!r)
  {

   cout<<"connect fail..."<<endl;
   exit(0);
  }
   srand(time(nullptr)^getpid());
   


   string oper="+-*/";
  int cnt=0;
  while(cnt<=10)
  {cout<<"-----------------------------"<<endl;
   int x=rand()%100+1;
   usleep(123);
   int y=rand()%100;
   char op=oper[rand()%oper.size()];
   
   Request st(x,y,op);
   string out;
   st.Serialize(&out);
   out=encode(out);
   
   write(sk.sockfd_,out.c_str(),out.size());
   char buffer[1024];
   int n=read(sk.sockfd_,buffer,sizeof(buffer));
   if(n>0)
     {

      buffer[n]=0;

      string info=buffer;
      string stream;
      bool r=decode(info,&stream);
      Response res;
      r=res.Deserialize(stream);
      assert(r);
      res.deprint();
      cout<<"-----------------------------"<<endl;

    
     }

   sleep(1);
   cnt++;
  }


  sk.Close(sk.sockfd_);

}
